package com.rede.didiok.user.modulelist.community.manager;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rede.didiok.common.constant.conf.SQLConf;
import com.rede.didiok.common.enums.BusinessTypeEnum;
import com.rede.didiok.common.enums.CommentSourceEnum;
import com.rede.didiok.common.enums.StatusEnum;
import com.rede.didiok.common.utils.StringUtils;
import com.rede.didiok.user.modulelist.community.entity.MomentCommentEntity;
import com.rede.didiok.user.modulelist.community.entity.UserMomentEntity;
import com.rede.didiok.user.modulelist.community.service.MomentCommentService;
import com.rede.didiok.user.modulelist.community.service.UserMomentService;

/**
 * 任务调度处理类
 *
 * @author 遇见
 * @date 2023年4月1日18:18:27
 */
@Service("communityTaskManager")
public class CommunityTaskManager {

    /**
     * 评论模块
     */
    @Resource
    MomentCommentService momentCommentService;

    /**
     * 用户动态模块
     */
    @Resource
    UserMomentService userMomentService;

    /**
     * 刷新评论数量的任务
     *
     * @param isAll
     */
    public void refreshCommentCountTask(boolean isAll) {
        // 获取最近一小时内变更的数量
        QueryWrapper<MomentCommentEntity> queryWrapper = new QueryWrapper<>();
        // 获取1小时前的日期
        if (!isAll) {
            Date date = new Date(System.currentTimeMillis() - 60 * 60 * 1000L);
            Date now = new Date();
            queryWrapper.between(SQLConf.CREATE_TIME, date, now);
        }
        queryWrapper.eq(SQLConf.SOURCE, CommentSourceEnum.RANK_MEMBER_INFO.getCode());
        queryWrapper.eq(SQLConf.STATUS, StatusEnum.ENABLE);
        queryWrapper.select("distinct blog_uid");
        List<MomentCommentEntity> commentList = momentCommentService.list(queryWrapper);
        List<String> resourceUidList = new ArrayList<>();
        for (MomentCommentEntity comment : commentList) {
            if (comment != null && StringUtils.isNotEmpty(comment.getMomentUid())) {
                resourceUidList.add(comment.getMomentUid());
            }
        }
        if (resourceUidList.size() == 0) {
            return;
        }
        // 获取对应
        queryWrapper.clear();
        queryWrapper.select("moment_uid as resource_uid", "count(1) as count");
        queryWrapper.in(SQLConf.MOMENT_UID, resourceUidList);
        queryWrapper.eq(SQLConf.STATUS, StatusEnum.ENABLE);
        queryWrapper.groupBy(SQLConf.RESOURCE_UID);
        List<Map<String, Object>> commentMap = momentCommentService.listMaps(queryWrapper);
        this.updateBlogInfo(commentMap, BusinessTypeEnum.USER_MOMENT_COMMENT);
    }

    private void updateBlogInfo(List<Map<String, Object>> updateMap, BusinessTypeEnum businessType) {
        for (Map<String, Object> map : updateMap) {
            String resourceUid = map.get(SQLConf.RESOURCE_UID).toString();
            Integer count = Integer.valueOf(map.get(SQLConf.COUNT).toString());
            UserMomentEntity userMoment = userMomentService.getById(resourceUid);
            if (userMoment == null) {
                continue;
            }
            switch (businessType) {

                case USER_MOMENT_COMMENT: {
                    userMoment.setCommentCount(count);
                }
                break;
            }
            userMomentService.updateById(userMoment);
        }
    }
}
